package com.wodwl.dao;

import java.io.IOException;

import java.io.Reader;

import java.util.List;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.wodwl.entity.User;

public class UserDao {

	public static enum Mode {

		INSERT, UPDATE_By_Uid, UPDATE_By_Username, DELETE

	}
	public void update(Object arg, Mode mode) {

		SqlMapClient smc = null;

		try {

			smc = getSqlMapClient();
			smc.startTransaction();

			switch (mode) {

			case INSERT:
				smc.insert("user.insertUser", arg);
				break;

			case UPDATE_By_Uid:
				smc.update("user.updateUserbyUid", arg);
				break;
			case UPDATE_By_Username:
				smc.update("user.updateUserbyUsername", arg);
				break;

			case DELETE:
				smc.delete("user.deleteUser", arg);
				break;

			}

			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}

	public List<User> listUsers() {
		SqlMapClient smc = null;
		List<User> users=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			users =smc.queryForList("user.getAllUsers",null);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return users;

	}


	public boolean checkUserExists(String username) {
		Integer count=0;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			count = (Integer) smc.queryForObject("user.checkUserExists", username);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		if(count>0)
			return true;
		else 
			return false;

	}
	public User selectUserByUid(int id) {
		User user = null;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			user = (User) smc.queryForObject("user.getUser", id);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		return user;

	}
	public User login(User user) {
		User result = null;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			result = (User) smc.queryForObject("user.login", user);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		return result;

	}
	public User selectUserByUsername(String username) {
		User user = null;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			user = (User) smc.queryForObject("user.getUserByUsername", username);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		return user;

	}
	
	public User getUserByUsernameAndPassword(User user) {
		User result = null;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			result = (User) smc.queryForObject("user.getUserByUsernameAndPassword", user);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		return result;

	}

	// //
	// ////////////////////////////////////////////////////////////////////////

	private SqlMapClient getSqlMapClient() throws IOException {

		// 初始化ibatis, 获得一个SqlMapClient对象

		String resource = "ibatis/SqlMapConfig.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		return SqlMapClientBuilder.buildSqlMapClient(reader);

	}

	private void endTransaction(SqlMapClient smc) {

		if (smc == null) {
			return;
		}
		try {
			smc.endTransaction();
		} catch (Exception e2) {
			e2.printStackTrace();
		}

	}

	public static void main(String[] args) {
		User user=new User();
		user.setUsername("aaa");
		user.setPassword("aaa");
		UserDao dao=new UserDao();
		User r=dao.login(user);
		System.out.println(r.getPassword());

	}

}